(function () {
    "use strict";

    angular.module('core').factory('notaFiscalService', factory);
    factory.$inject = ['$http', '$q'];

    function factory($http, $q) {

        return {
            get: function (id) {
                var deferred = $q.defer();
                $http.post('/notafiscal/get', id)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
            getAll: function (args) {
                var deferred = $q.defer();
                $http.post('/notafiscal/getall', args)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
            count: function (args) {
                var deferred = $q.defer();
                $http.post('/notafiscal/count', args)
                    .success(function (data, status, headers, config) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },

            getXmlContentBase64: function (args) {
                var deferred = $q.defer();
                $http.post('/notafiscal/getXmlContentBase64', args)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
            getXmlContentBase64Cancelamento: function (args) {
                var deferred = $q.defer();
                $http.post('/notafiscal/getXmlContentBase64Cancelamento', args)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
            getXmlContentBase64Inutilizacao: function (args) {
                var deferred = $q.defer();
                $http.post('/notafiscal/getXmlContentBase64Inutilizacao', args)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
            downloadAllXml: function (args) {
                var deferred = $q.defer();
                $http.post('/notafiscal/downloadallxml', args, { responseType: 'arraybuffer' })
                    .success(function (data, status, headers, config) { deferred.resolve({ data, status, headers, config }); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },

            save: function (item) {
                var deferred = $q.defer();
                $http.post('/notafiscal/save', item)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
            delete: function (id) {
                var deferred = $q.defer();
                $http.post('/notafiscal/delete', id)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },

            enviarEmissaoSefaz: function (item) {
                var deferred = $q.defer();
                $http.post('/notafiscal/enviarEmissaoSefaz', item)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
            enviarCancelamentoSefaz: function (item) {
                var deferred = $q.defer();
                $http.post('/notafiscal/enviarCancelamentoSefaz', item)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
            enviarInutilizacaoSefaz: function (item) {
                var deferred = $q.defer();
                $http.post('/notafiscal/enviarInutilizacaoSefaz', item)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
            enviarConsultaSefaz: function (item) {
                var deferred = $q.defer();
                $http.post('/notafiscal/enviarConsultaSefaz', item)
                    .success(function (data) { deferred.resolve(data); })
                    .error(function (data, status, headers, config) { deferred.reject(data); });
                return deferred.promise;
            },
        };
    }

})();


